{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-25T10:47:14.140461Z",
     "start_time": "2025-09-25T10:47:14.079475Z"
    }
   },
   "source": [
    "# numpy学习\n",
    "from numpy.conftest import dtype"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-26T07:33:35.769836Z",
     "start_time": "2025-09-26T07:33:35.766342Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "# 创建一个数组\n",
    "arr = np.array([[1, 2, 3, 4, 5],[1, 2, 3, 4, 5]])\n",
    "print(arr.ndim)#获取数组的维度"
   ],
   "id": "8dec379a887dec9c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-26T07:33:11.489675Z",
     "start_time": "2025-09-26T07:33:11.484468Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#同质性\n",
    "arr = np.array([1, 'string'])\n",
    "print(arr)\n",
    "arr = np.array([1,2.5,3],dtype= np.int64)\n",
    "print(arr)"
   ],
   "id": "4fb4ae4ef644fb1a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1' 'string']\n",
      "[1 2 3]\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-25T10:55:21.961475Z",
     "start_time": "2025-09-25T10:55:21.958452Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#ndarray的属性\n",
    "arr = np.array([[1, 2, 3, 4, 5],[1, 2, 5, 4, 5]])\n",
    "arr = np.array(0)\n",
    "print(arr.ndim)\n",
    "print(arr.shape)\n",
    "print(arr.size)\n",
    "print(arr.dtype)\n",
    "print(arr.itemsize)#每个元素的字节数\n",
    "print(arr.nbytes)#数组字节数\n",
    "print(arr.T)"
   ],
   "id": "f306995da6c202a2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "()\n",
      "1\n",
      "int64\n",
      "8\n",
      "8\n",
      "0\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "ndaeeay的创建方式",
   "id": "436454b1eef64ac4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-25T10:59:17.047782Z",
     "start_time": "2025-09-25T10:59:17.044583Z"
    }
   },
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 3. 4. 5.]\n"
     ]
    }
   ],
   "execution_count": 17,
   "source": [
    "#ndarray的创建方式\n",
    "list = [1,2,3,4,5]\n",
    "arr = np.array(list,dtype= np.float64)#基础方法\n",
    "print(arr)"
   ],
   "id": "a47e67b339afe6c9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-25T11:01:47.071926Z",
     "start_time": "2025-09-25T11:01:47.069381Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#copy\n",
    "arr1 = np.copy(arr)\n",
    "print(arr1)\n",
    "arr1[0]=5\n",
    "print(arr)\n",
    "print(arr1)"
   ],
   "id": "8ddd6d2f987df39b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 3. 4. 5.]\n",
      "[1. 2. 3. 4. 5.]\n",
      "[5. 2. 3. 4. 5.]\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-25T11:07:18.992365Z",
     "start_time": "2025-09-25T11:07:18.989835Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#预定义形状\n",
    "#全0 全1 固定值\n",
    "#0\n",
    "arr = np.zeros((3,2),dtype= np.int64)\n",
    "print(arr)"
   ],
   "id": "140302e42995084",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0]\n",
      " [0 0]\n",
      " [0 0]]\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-25T11:08:16.280Z",
     "start_time": "2025-09-25T11:08:16.276857Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#1\n",
    "arr = np.ones((2,4),dtype = np.int64)\n",
    "print(arr)"
   ],
   "id": "b19c81391b26bf36",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1 1 1]\n",
      " [1 1 1 1]]\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-25T11:08:46.594828Z",
     "start_time": "2025-09-25T11:08:46.591757Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#固定值\n",
    "arr = np.full((2,5),14)\n",
    "print(arr)"
   ],
   "id": "eedd386ac126a1f9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[14 14 14 14 14]\n",
      " [14 14 14 14 14]]\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-25T11:08:51.766777Z",
     "start_time": "2025-09-25T11:08:51.764675Z"
    }
   },
   "cell_type": "code",
   "source": "#like",
   "id": "16b739e600c88e93",
   "outputs": [],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-25T11:09:55.438998Z",
     "start_time": "2025-09-25T11:09:55.435794Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.zeros_like(arr1)\n",
    "print(arr)\n",
    "arr = np.ones_like(arr1)\n",
    "print(arr)\n",
    "arr = np.full_like(arr1,14)\n",
    "print(arr)"
   ],
   "id": "ca7bae21a714e1fa",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 0. 0. 0. 0.]\n",
      "[1. 1. 1. 1. 1.]\n",
      "[14. 14. 14. 14. 14.]\n"
     ]
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-25T11:21:56.862607Z",
     "start_time": "2025-09-25T11:21:56.859401Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#等差数列\n",
    "arr = np.arange(1,21,5)#参数21表示结束,默认是步长为5\n",
    "print(arr)#不带结束\n",
    "arr = np.arange(35,15,-4)\n",
    "print(arr)"
   ],
   "id": "6d4791a45e5d182a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  6 11 16]\n",
      "[35 31 27 23 19]\n"
     ]
    }
   ],
   "execution_count": 44
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-25T11:25:18.957148Z",
     "start_time": "2025-09-25T11:25:18.954074Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#等间隔\n",
    "arr = np.linspace(1,21,3)#参数3表示等间隔数,默认是等分\n",
    "print(arr)\n",
    "arr = np.linspace(1,21,4,endpoint= False)#endpoint= False表示不包含结束\n",
    "print(arr)"
   ],
   "id": "dff79f9ca56ddbe0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1. 11. 21.]\n",
      "[ 1.  6. 11. 16.]\n"
     ]
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-25T11:41:23.679586Z",
     "start_time": "2025-09-25T11:41:23.676334Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#对数\n",
    "arr = np.logspace(1,5,3,base= 2)#参数3表示等间隔数,base= 2表示底数是2\n",
    "print(arr)"
   ],
   "id": "d6fe58e7542f8c76",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 2.  8. 32.]\n"
     ]
    }
   ],
   "execution_count": 67
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-26T07:33:40.824867Z",
     "start_time": "2025-09-26T07:33:40.820365Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#构造特殊数组\n",
    "#单位矩阵\n",
    "arr = np.eye(3,dtype= np.int64)\n",
    "print(arr)\n",
    "arr = np.eye(3,4,dtype= np.int64)\n",
    "print(arr)"
   ],
   "id": "233a20617d0453b0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 0 0]\n",
      " [0 1 0]\n",
      " [0 0 1]]\n",
      "[[1 0 0 0]\n",
      " [0 1 0 0]\n",
      " [0 0 1 0]]\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-25T11:45:44.610130Z",
     "start_time": "2025-09-25T11:45:44.606477Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#对角矩阵\n",
    "arr = np.diag([1,2,3])\n",
    "print(arr)\n",
    "\n",
    "arr = np.diag([1,2,3],1)#参数1表示对角线向右移动一位\n",
    "print(arr)"
   ],
   "id": "99c65978f87bc052",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 0 0]\n",
      " [0 2 0]\n",
      " [0 0 3]]\n",
      "[[0 1 0 0]\n",
      " [0 0 2 0]\n",
      " [0 0 0 3]\n",
      " [0 0 0 0]]\n"
     ]
    }
   ],
   "execution_count": 77
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-25T11:49:52.717898Z",
     "start_time": "2025-09-25T11:49:52.714636Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#随机数\n",
    "arr = np.random.rand(2,3)#功能随机生成0-1的数组\n",
    "print(arr)\n",
    "arr = np.random.uniform(1,10,(2,3))#功能随机生成1-10的浮点数数组\n",
    "print(arr)"
   ],
   "id": "72f28b031b4161",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.30234003 0.95675252 0.79826654]\n",
      " [0.60217977 0.35927477 0.08115791]]\n",
      "[[2.14872287 2.87179241 8.89548974]\n",
      " [7.10655037 1.935421   2.53465916]]\n"
     ]
    }
   ],
   "execution_count": 86
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-25T11:51:03.762268Z",
     "start_time": "2025-09-25T11:51:03.759683Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.random.randint(1,10,(2,3))\n",
    "print(arr)"
   ],
   "id": "caec113f80b968bf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4 3 5]\n",
      " [3 2 9]]\n"
     ]
    }
   ],
   "execution_count": 91
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-26T07:33:44.705556Z",
     "start_time": "2025-09-26T07:33:44.435370Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#正态分布\n",
    "arr = np.random.randn(2,3)\n",
    "print(arr)\n",
    "#只能生成绝对值0-3的数组"
   ],
   "id": "4bd8684a2ca375aa",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.00630165 -1.33131075 -1.40328817]\n",
      " [-0.02154903 -0.11294737 -0.92531134]]\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-26T07:33:46.313778Z",
     "start_time": "2025-09-26T07:33:46.307851Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#随机种子\n",
    "#np.random.seed(0)\n",
    "arr = np.random.randint(2,30,(2,))\n",
    "print(arr)"
   ],
   "id": "3591668bd832c83f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[14 17]\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-26T07:51:41.596571Z",
     "start_time": "2025-09-26T07:51:41.593026Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#数组的索引\n",
    "#一维数组\n",
    "arr = np.random.randint(1,10,20)\n",
    "print(arr)\n",
    "print(arr[:])#打印整个数组\n",
    "print(arr[3:])\n",
    "print(arr[arr>=5])\n",
    "print(arr[-3:-1])"
   ],
   "id": "407c32f86d8f492f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 4 4 3 4 5 2 3 2 5 7 9 3 4 1 1 7 1 7 4]\n",
      "[3 4 4 3 4 5 2 3 2 5 7 9 3 4 1 1 7 1 7 4]\n",
      "[3 4 5 2 3 2 5 7 9 3 4 1 1 7 1 7 4]\n",
      "[5 5 7 9 7 7]\n",
      "[1 7]\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-26T07:59:51.977192Z",
     "start_time": "2025-09-26T07:59:51.973160Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#二维数组\n",
    "arr = np.random.randint(1,10,(3,4))\n",
    "print(arr)\n",
    "print(arr[:,:])#打印整个数组\n",
    "print(arr[0,:])#打印第一行\n",
    "print(arr[:,0])#打印第一列\n",
    "print(arr[0:2,2:4])#打印第一行和第二行的第三，四列\n",
    "print(arr[arr>5])\n",
    "print(arr[0][arr[0]>5])"
   ],
   "id": "69c9971752d57977",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4 8 6 4]\n",
      " [5 6 4 4]\n",
      " [8 8 4 3]]\n",
      "[[4 8 6 4]\n",
      " [5 6 4 4]\n",
      " [8 8 4 3]]\n",
      "[4 8 6 4]\n",
      "[4 5 8]\n",
      "[[6 4]\n",
      " [4 4]]\n",
      "[8 6 6 8 8]\n",
      "[8 6]\n"
     ]
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-26T08:13:56.275853Z",
     "start_time": "2025-09-26T08:13:56.271585Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#darray数组的计算\n",
    "arr1 = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "arr2 = np.array([[5,8,7],[2,5,5],[3,8,4]])\n",
    "print(arr1)\n",
    "print(arr2)\n",
    "print(arr1 + arr2)\n",
    "print(arr1 - arr2)\n",
    "print(arr1 * arr2)\n",
    "print(arr1 / arr2)\n",
    "print(arr1 % arr2)\n",
    "print(arr1 ** arr2)\n",
    "print(arr1 // arr2)#整除除法\n",
    "print(arr1 @ arr2)#矩阵乘法\n",
    "print(arr1[0,:]+arr2[:,:])"
   ],
   "id": "15e72506b269ef29",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "[[5 8 7]\n",
      " [2 5 5]\n",
      " [3 8 4]]\n",
      "[[ 6 10 10]\n",
      " [ 6 10 11]\n",
      " [10 16 13]]\n",
      "[[-4 -6 -4]\n",
      " [ 2  0  1]\n",
      " [ 4  0  5]]\n",
      "[[ 5 16 21]\n",
      " [ 8 25 30]\n",
      " [21 64 36]]\n",
      "[[0.2        0.25       0.42857143]\n",
      " [2.         1.         1.2       ]\n",
      " [2.33333333 1.         2.25      ]]\n",
      "[[1 2 3]\n",
      " [0 0 1]\n",
      " [1 0 1]]\n",
      "[[       1      256     2187]\n",
      " [      16     3125     7776]\n",
      " [     343 16777216     6561]]\n",
      "[[0 0 0]\n",
      " [2 1 1]\n",
      " [2 1 2]]\n",
      "[[ 18  42  29]\n",
      " [ 48 105  77]\n",
      " [ 78 168 125]]\n",
      "[[ 6 10 10]\n",
      " [ 3  7  8]\n",
      " [ 4 10  7]]\n"
     ]
    }
   ],
   "execution_count": 47
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-26T09:44:42.823032Z",
     "start_time": "2025-09-26T09:44:42.809981Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#numpy的基本数学函数\n",
    "arr = np.arange(0,2*np.pi+np.pi/2,np.pi/2)\n",
    "print(arr)\n",
    "print(np.sin(arr))"
   ],
   "id": "67071348d5e8a207",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.         1.57079633 3.14159265 4.71238898 6.28318531]\n",
      "[ 0.0000000e+00  1.0000000e+00  1.2246468e-16 -1.0000000e+00\n",
      " -2.4492936e-16]\n"
     ]
    }
   ],
   "execution_count": 52
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-26T10:01:27.966216Z",
     "start_time": "2025-09-26T10:01:27.958532Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#统计函数\n",
    "arr = np.random.randint(1,10,10)\n",
    "print(arr)\n",
    "print(np.sum(arr))\n",
    "print(np.min(arr))\n",
    "print(np.max(arr))\n",
    "print(np.mean(arr))\n",
    "print(np.median(arr))\n",
    "print(np.std(arr))\n",
    "print(np.var(arr))\n",
    "print(np.argmax(arr))\n",
    "print(np.argmin(arr))\n",
    "print(np.cumsum( arr))\n",
    "print(np.cumprod( arr))\n",
    "print(np.percentile(arr,50))\n",
    "print(np.isnan(arr))"
   ],
   "id": "242a84513e12974f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[9 5 1 3 6 6 1 9 2 2]\n",
      "44\n",
      "1\n",
      "9\n",
      "4.4\n",
      "4.0\n",
      "2.90516780926679\n",
      "8.440000000000001\n",
      "0\n",
      "2\n",
      "[ 9 14 15 18 24 30 31 40 42 44]\n",
      "[     9     45     45    135    810   4860   4860  43740  87480 174960]\n",
      "4.0\n",
      "[False False False False False False False False False False]\n"
     ]
    }
   ],
   "execution_count": 55
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-26T10:14:08.849499Z",
     "start_time": "2025-09-26T10:14:08.844214Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#数组比较函数\n",
    "arr1 = np.random.randint(50,100,10)\n",
    "arr2 = np.random.randint(50,100,10)\n",
    "print(arr1)\n",
    "print(arr2)\n",
    "print(np.greater(arr1,arr2))\n",
    "print(np.less(arr1,arr2))\n",
    "print(np.equal(arr1,arr2))\n",
    "print(np.greater(arr1,80))\n",
    "print(np.less(arr1,80))\n",
    "print(np.equal(arr1,80))\n",
    "print(np.where(arr1>80))\n",
    "print(np.where(arr1>80,arr1,arr2))\n",
    "print(np.logical_and(arr1>80,arr2>80))\n",
    "print(np.logical_or(arr1<60,arr2>80))\n",
    "print(np.logical_not(arr1>80))\n",
    "print(np.where(arr1<60,\"不及格\",np.where(arr1>80,\"优秀\",\"良好\")))\n",
    "print(np.select([arr1>80,arr1<60,(arr1>60)&(arr1<80)],[\"优秀\",\"不及格\",\"良好\"],default=\"良好\"))\n",
    "print(np.any(arr1>80))\n",
    "print(np.all(arr1>80))"
   ],
   "id": "ca033b35ccfabce9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[88 70 57 69 81 50 55 77 93 80]\n",
      "[59 69 57 71 87 78 58 93 96 50]\n",
      "[ True  True False False False False False False False  True]\n",
      "[False False False  True  True  True  True  True  True False]\n",
      "[False False  True False False False False False False False]\n",
      "[ True False False False  True False False False  True False]\n",
      "[False  True  True  True False  True  True  True False False]\n",
      "[False False False False False False False False False  True]\n",
      "(array([0, 4, 8]),)\n",
      "[88 69 57 71 81 78 58 93 93 50]\n",
      "[False False False False  True False False False  True False]\n",
      "[False False  True False  True  True  True  True  True False]\n",
      "[False  True  True  True False  True  True  True False  True]\n",
      "['优秀' '良好' '不及格' '良好' '优秀' '不及格' '不及格' '良好' '优秀' '良好']\n",
      "['优秀' '良好' '不及格' '良好' '优秀' '不及格' '不及格' '良好' '优秀' '良好']\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "execution_count": 68
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-26T11:16:01.121940Z",
     "start_time": "2025-09-26T11:16:01.114950Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#数组的排序\n",
    "arr = np.random.randint(1,10,(2,3))\n",
    "print(arr)\n",
    "print(np.sort(arr))\n",
    "print(np.sort(arr,axis=0))#按行排序\n",
    "print(np.sort(arr,axis=1))\n",
    "print(np.argsort(arr))\n",
    "#数组拼接和切割\n",
    "arr1 = np.random.randint(1,10,(2,3))\n",
    "arr2 = np.random.randint(1,10,(2,3))\n",
    "print(\"拼接\")\n",
    "print(arr1)\n",
    "print()\n",
    "print(arr2)\n",
    "print(\"垂直拼接\")\n",
    "a=np.concatenate((arr1,arr2),axis=0)\n",
    "print(a)#垂直拼接\n",
    "print(\"水平拼接\")\n",
    "b=np.concatenate((arr1,arr2),axis=1)\n",
    "print(b)#水平拼接\n",
    "print(np.vstack((arr1,arr2)))#垂直拼接\n",
    "print(np.hstack((arr1,arr2)))#水平拼接\n",
    "#print(np.split(arr1,2,axis=1))#水平切分,axis=1表示水平切分\n",
    "print()\n",
    "print(np.array_split(a,2,axis=1))#垂直切分,axis=1表示垂直切分\n",
    "print(np.array_split(b,2,axis=0))#水平切分,axis=0表示水平切分\n",
    "print()\n",
    "print(np.split(arr1,2))\n",
    "print(np.split(arr1,3,1))\n",
    "print()\n",
    "print(np.vsplit(arr1,2))\n",
    "print(np.hsplit(arr1,3))"
   ],
   "id": "33d200a44d6546c8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[6 1 4]\n",
      " [8 1 5]]\n",
      "[[1 4 6]\n",
      " [1 5 8]]\n",
      "[[6 1 4]\n",
      " [8 1 5]]\n",
      "[[1 4 6]\n",
      " [1 5 8]]\n",
      "[[1 2 0]\n",
      " [1 2 0]]\n",
      "拼接\n",
      "[[2 4 6]\n",
      " [8 7 3]]\n",
      "\n",
      "[[8 4 5]\n",
      " [8 1 1]]\n",
      "垂直拼接\n",
      "[[2 4 6]\n",
      " [8 7 3]\n",
      " [8 4 5]\n",
      " [8 1 1]]\n",
      "水平拼接\n",
      "[[2 4 6 8 4 5]\n",
      " [8 7 3 8 1 1]]\n",
      "[[2 4 6]\n",
      " [8 7 3]\n",
      " [8 4 5]\n",
      " [8 1 1]]\n",
      "[[2 4 6 8 4 5]\n",
      " [8 7 3 8 1 1]]\n",
      "\n",
      "[array([[2, 4],\n",
      "       [8, 7],\n",
      "       [8, 4],\n",
      "       [8, 1]], dtype=int32), array([[6],\n",
      "       [3],\n",
      "       [5],\n",
      "       [1]], dtype=int32)]\n",
      "[array([[2, 4, 6, 8, 4, 5]], dtype=int32), array([[8, 7, 3, 8, 1, 1]], dtype=int32)]\n",
      "\n",
      "[array([[2, 4, 6]], dtype=int32), array([[8, 7, 3]], dtype=int32)]\n",
      "[array([[2],\n",
      "       [8]], dtype=int32), array([[4],\n",
      "       [7]], dtype=int32), array([[6],\n",
      "       [3]], dtype=int32)]\n",
      "\n",
      "[array([[2, 4, 6]], dtype=int32), array([[8, 7, 3]], dtype=int32)]\n",
      "[array([[2],\n",
      "       [8]], dtype=int32), array([[4],\n",
      "       [7]], dtype=int32), array([[6],\n",
      "       [3]], dtype=int32)]\n"
     ]
    }
   ],
   "execution_count": 107
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-26T12:08:27.023561Z",
     "start_time": "2025-09-26T12:08:27.018126Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#练习\n",
    "arr = np.random.randint(0,10,(3,4))\n",
    "print(arr)\n",
    "for i in range(3):\n",
    "    print(np.max(arr[i]))\n",
    "for i in range(4):\n",
    "    print(np.min(arr[:,i]))\n",
    "print()\n",
    "print(np.max(arr,axis=0))\n",
    "print()\n",
    "print(np.min(arr,axis=1))\n",
    "print()\n",
    "arr = np.where(arr%2!=0,-1,arr)\n",
    "print(arr)\n",
    "\n",
    "a = np.random.randint(0,10,(2,2))\n",
    "b = np.random.randint(0,10,(2,2))\n",
    "print(a)\n",
    "print(b)\n",
    "print(np.concatenate((a,b),axis=0))\n",
    "print(np.concatenate((a,b),axis=1))\n",
    "print(np.vstack((a,b)))\n",
    "print(np.hstack((a,b)))"
   ],
   "id": "b34690b2567275b8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 9 3]\n",
      " [8 3 0 4]\n",
      " [4 0 2 5]]\n",
      "9\n",
      "8\n",
      "5\n",
      "0\n",
      "0\n",
      "0\n",
      "3\n",
      "\n",
      "[8 3 9 5]\n",
      "\n",
      "[0 0 0]\n",
      "\n",
      "[[ 0  0 -1 -1]\n",
      " [ 8 -1  0  4]\n",
      " [ 4  0  2 -1]]\n",
      "[[5 8]\n",
      " [2 7]]\n",
      "[[3 6]\n",
      " [1 0]]\n",
      "[[5 8]\n",
      " [2 7]\n",
      " [3 6]\n",
      " [1 0]]\n",
      "[[5 8 3 6]\n",
      " [2 7 1 0]]\n",
      "[[5 8]\n",
      " [2 7]\n",
      " [3 6]\n",
      " [1 0]]\n",
      "[[5 8 3 6]\n",
      " [2 7 1 0]]\n"
     ]
    }
   ],
   "execution_count": 118
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-26T12:11:30.053289Z",
     "start_time": "2025-09-26T12:11:30.049833Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "a = pd.Series([1,2,3,4,5],index=['a','b','c','d','e'],name='series')\n",
    "print(a)\n",
    "print(a[2:-1].sum())"
   ],
   "id": "c3c72fc0358b2a7b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "d    4\n",
      "e    5\n",
      "Name: series, dtype: int64\n",
      "7\n"
     ]
    }
   ],
   "execution_count": 123
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
